/*=========== initing start ============*/
	var aParams = getURIParams();
	aParams.courseId = parseInt(aParams.courseId);
	/*-------- detail wrapper ----------*/
	var oDetailWrapper = document.getElementById("course-detail-wrapper");
	var oCourseTitle = oDetailWrapper.getElementsByTagName("h2")[0];
	var aDetailSpan = oDetailWrapper.getElementsByTagName("span");
	var oTeacherField = document.getElementById("teacher-field");
	var oTeacherHead = oTeacherField.getElementsByTagName("img")[0];
	var oTeacherName = getElementsByClass("teacher-name",oTeacherField)[0];
	var oJoinWrapper = getElementsByClass("join-wrapper",oDetailWrapper)[0];
	var oJoin = getElementsByClass("join",oJoinWrapper)[0];
	var oCollect = getElementsByClass("collect",oJoinWrapper)[0];
	/*---------- course infor main -----------*/
	var oCourseIntro = document.getElementById("course-intro");
	var oCourseMenu = document.getElementById("course-menu");
	var aCourseMenuLis = oCourseMenu.getElementsByTagName("li");
	/*---- chapters ---*/
	var oChapterWrapper = document.getElementById("course-chapters");

	var isAttended = false;
	var isCollected = false;
	var courseInfor = {};

	function askCourseData(){
		ajax({
			type:"post",
			url:"/cyl/getCourseInfo",
			data:{
				email:getCookie("useremail"),
				courseId:aParams.courseId
			},
			success:askCourseDataSuccess,
			fail:askDataFail
		});
	}
	function askCourseDataSuccess(response){
		var respJson = JSON.parse(response);
		console.log(respJson);
		console.log(typeof respJson[0]);
		isAttended = respJson[0].isAttend == 0?false:true;
		isCollected = respJson[1].isCollect == 0?false:true;
		showAttend();
		showCollect();
		courseInfor = respJson[2];
		oCourseTitle.innerText = courseInfor.coursename; //课程标题
		aDetailSpan[0].innerText = courseInfor.students;//学习人数
		aDetailSpan[1].innerText = courseInfor.degree; //难度
		aDetailSpan[2].innerText = courseInfor.time || ""; //时长
		aDetailSpan[3].innerText = courseInfor.score || ""; //评分
		oCourseIntro.innerText = courseInfor.detail; //简介
		oTeacherName.innerText = respJson[3].username;//教师名
		askChapterInfor();
	}
	function askChapterInfor(){
		console.log(111);
		ajax({
			type:"post",
			url:"/cyl/getLesson",
			data:{
				email:getCookie("useremail"),
				courseId:aParams.courseId
			},
			success:askChapterInforSuccess,
			fail:askDataFail
		});
	}
	function askChapterInforSuccess(response){
		var respJson = JSON.parse(response);
		console.log(respJson);
		for(var i=0;i<respJson.length;i++){
			var chapter = createChapter();
			chapter.link.href = "video.html?url="+respJson[i].url; //视频地址
			chapter.title.innerText = "第"+(i+1)+"节"+respJson[i].name;//章节名
			oChapterWrapper.appendChild(chapter.wrapper);
		}
	}
	function askDataFail(){

	}
/*=========== initing end ============*/

/*============= main start =============*/

	/*-------- detail wrapper ----------*/
	//参加学习
	console.log(oJoin);
	oJoin.onclick = function(){
		if(isLogined()){
			ajax({
				type:"post",
				url:"/cyl/updateStatus",
				data:{
					email:getCookie("useremail"),
					courseId:aParams.courseId,
					attend:isAttended?0:1
				},
				success:askAttendSuccess,
				fail:askAttendFail
			});
		}else{
			showLoginBox();
		}
	}
	//收藏
	oCollect.onclick = function(){
		if(isLogined()){
			ajax({
				type:"post",
				url:"/cyl/updateStatus",
				data:{
					email:getCookie("useremail"),
					courseId:aParams.courseId,
					collect:isCollected?0:1
				},
				success:askCollectSuccess,
				fail:askCollectFail
			});
		}else{
			showLoginBox();
		}
	}
	function askAttendSuccess(response){
		var respJson = JSON.parse(response);
		if(respJson.status == 1){
			isAttended = !isAttended;
			showAttend();
		}
	}
	function askAttendFail(response){
		var respJson = JSON.parse(response);

	}
	function askCollectSuccess(response){
		var respJson = JSON.parse(response);
		if(respJson.status == 1){
			isCollected = !isCollected;
			showCollect();
		}
	}
	function askCollectFail(response){

	}
	function showAttend(){
		if(isAttended){
			oJoin.innerText = "立即学习";
		}else{
			oJoin.innerText = "加入学习";
		}
	}
	function showCollect(){
		if(isCollected){
			oCollect.className = "collect iconfont icon-star"
		}else{
			oCollect.className = "collect iconfont icon-star1"
		}
	}
	/*------- chapter --------*/
	function createChapter(){
		var chapterWrapper = document.createElement("div");
		chapterWrapper.className = "chapter";
		var link = document.createElement("a");
		var chapterIcon = document.createElement("span");
		chapterIcon.className = "chapter-icon";
		chapterIcon.innerText = "=";
		var title = document.createElement("h3");
		title.className = "title";
		var descBox = document.createElement("div");
		descBox.className = "desc-box";
		var descIcon = document.createElement("span");
		descIcon.className = "desc-icon";
		descIcon.innerText = "!";
		var chapterDesc = document.createElement("div");
		chapterDesc.className = "chapter-desc";
		var startBtn = document.createElement("button");
		startBtn.innerText = "开始学习";
		descBox.appendChild(descIcon);
		descBox.appendChild(chapterDesc);
		link.appendChild(chapterIcon);
		link.appendChild(title);
		link.appendChild(descBox);
		link.appendChild(startBtn);
		chapterWrapper.appendChild(link);
		var json = {
			wrapper:chapterWrapper,
			link:link,
			title:title,
			desc:chapterDesc,
			startBtn:startBtn
		};
		return json;
	}
	function addChapter(respJson){

	}
/*============= main end =============*/
	/*-------- common ----------*/
	askCourseData();
